<?php
class masterbarangdetailcontroller extends CI_Controller
{
	function __construct()
	{
		parent::__construct();
		$this->load->helper('form');
		$this->load->helper('currupiah');
		$this->load->library('form_validation');		
		$this->load->model('masterbarang_model');
		$this->load->model('masterbarangdetail_model');
		$this->load->library('session');
		$this->load->library('pagination_editable');
		if(($this->session->userdata('fullname') == '') || ($this->session->userdata('fullname') == NULL)){
			redirect('logincontroller');
		}
	}
	
	function index()
	{		
		$idkddet = $this->uri->segment(3);
		
		if (isset($_POST['btnview'])) 
		{
			if($_POST['kdtransview'] == "0")
			{         
				$dataprofile = array(
					'page_title' => 'Master > Barang Detail',
					'name' => $this->session->userdata('fullname'),
					'kdbrgheader' => $this->masterbarangdetail_model->getBarang($_POST['kdbrngview']),
					'mstrbarangdet' => $this->masterbarangdetail_model->getList($_POST['kdbrngview']),
					'message' =>''		
				);		
			}
			else if($_POST['kdbrgview'] == "1")
			{
				$this->masterbarangdetail_model->setKodeDetailBarang($_POST['brgview']);
				$dataprofile = array(
					'page_title' => 'Master > Barang Detail',
					'name' => $this->session->userdata('fullname'),
					'kdbrgheader' => $this->masterbarangdetail_model->getBarang($_POST['kdbrngview']),
					'mstrbarangdet' => $this->masterbarangdetail_model->getkdbaranglike(),
					'message' =>''	
				);
			}
			else if($_POST['kdbrgview'] == "2")
			{
				$this->masterbarangdetail_model->setPruductname($_POST['brgview']);
				$dataprofile = array(
					'page_title' => 'Master > Barang Detail',
					'name' => $this->session->userdata('fullname'),
					'kdbrgheader' => $this->masterbarangdetail_model->getBarang($_POST['kdbrngview']),
					'mstrbarangdet' => $this->masterbarangdetail_model->getnamabaranglike(),
					'message' =>''			
				);
			}
		}
		else
		{
			$dataprofile = array(
				'page_title' => 'Master > Barang Detail',
				'name' => $this->session->userdata('fullname'),
				'kdbrgheader' => $this->masterbarangdetail_model->getBarang($idkddet),
				'mstrbarangdet' => $this->masterbarangdetail_model->getList($idkddet),
				'message' => ''	
			);	
		}	
		$content = array(
			'content' => 'master/barang/detailview'
		);
		$this->template->load('templates/default',$content,$dataprofile);	
	}
	
	function viewdetail() 
	{
		$idkddet = $this->uri->segment(3);
		$this->masterbarangdetail_model->setKodeBarang($idkddet);

		$dataprofile = array(
			'page_title' => 'Master > Barang Detail',
			'name' => $this->session->userdata('fullname'),
			'kdbrgheader' => $this->masterbarangdetail_model->getBarang($idkddet),
			'mstrbarangdet' => $this->masterbarangdetail_model->getList($idkddet),	
			'message' => ''			
		);		
		
		$content = array(
			'content' => 'master/barang/detailview'
		);
		$this->template->load('templates/default',$content,$dataprofile);
	}
	
	function tambahmasterbarang() 
	{
        if (isset($_POST['btnSave'])) 
		{	
			if (isset($_POST['kdbrngdet']) &&
					isset($_POST['nmbrng']) &&
                    isset($_POST['description']) &&
					isset($_POST['tpunit']) &&
                    !($_POST['kdbrngdet'] == '') &&
                    !($_POST['nmbrng'] == '') &&
					!($_POST['description'] == '') &&
                    !($_POST['tpunit'] == '')) 
			 {
				/*$query = $this->db->query("select count(KodeBarang) as kdbrg from product_header where KodeBarang='"& $_POST['kdbrng'] &"'");
				$kd = "";
				if($query->num_rows()>0)
				{
					$dataprofile = array(
						'page_title' => 'Master > Barang',
            			'name' => $this->session->userdata('fullname'),
						'message' => '<div style="color:red;font-size:9pt;font-weight:bold;">Kode Barang sudah pernah terpakai </div>',
						'mstrbarangdet' => $this->masterbarangdetail_model->getList());
                	$content = array(
                    	'content' => 'master/barang/view');
                	$this->template->load('template/default', $content, $dataprofile);
				}
				else
				{*/
					$this->masterbarangdetail_model->setKodeBarang($_POST['kdbrng']);
					$this->masterbarangdetail_model->setKodeDetailBarang($_POST['kdbrngdet']);
					$this->masterbarangdetail_model->setProductname($_POST['nmbrng']);
					$this->masterbarangdetail_model->setTypeunit($_POST['tpunit']);
					$this->masterbarangdetail_model->setDescription($_POST['description']);
					$this->masterbarangdetail_model->setItemprice($_POST['itmprice']);
					$this->masterbarangdetail_model->setCreatedBy($this->session->userdata('userid'));
					$tanggal=date('Y-m-d H:i:s', strtotime('now'));
					$this->masterbarangdetail_model->setCreatedOn($tanggal);
					$this->masterbarangdetail_model->setLastUpdateBy($this->session->userdata('userid'));
					$this->masterbarangdetail_model->setLastUpdateOn($tanggal);
					
					$this->masterbarangdetail_model->insert();
					
					redirect('masterbarangdetailcontroller/viewdetail/'.$_POST['kdbrng']);
				//}
            } 
			else 
			{
                $dataprofile = array(
					'page_title' => 'Master > Barang Detail',
            		'name' => $this->session->userdata('fullname'),
					'kdbrgheader' => $this->masterbarangdetail_model->getBarang($_POST['kdbrng']),
					'message' => '<div style="color:red;font-size:9pt;font-weight:bold;">Lengkapi Form Terlebih Dahulu Untuk Menambah Master Barang Detail</div>',
					'mstrbarangdet' => $this->masterbarangdetail_model->getList($_POST['kdbrng'])
                );
                $content = array(
                    'content' => 'master/barang/detailview'
                );
                $this->template->load('templates/default', $content, $dataprofile);
            }
        } else {
           $dataprofile = array(
			'page_title' => 'Master > Barang Detail',
			'name' => $this->session->userdata('fullname'),
			'kdbrgheader' => $this->masterbarangdetail_model->getBarang($_POST['kdbrng']),
			'mstrbarangdet' => $this->masterbarangdetail_model->getList($_POST['kdbrng']),
			'message' =>''		
			);		
			$content = array(
				'content' => 'master/barang/detailview'
			);
			$this->template->load('templates/default',$content,$dataprofile);
        }
    }
	
	function ubahdetailbarang()
	{
		$idkddet = $this->uri->segment(3);
		$this->masterbarangdetail_model->setKodeDetailBarang($idkddet);
        if (isset($_POST['btnSave'])) 
		{
			
			$this->masterbarangdetail_model->setKodeBarang($_POST['kdbrng']);
			$this->masterbarangdetail_model->setKodeDetailBarang($_POST['kdbrngdet']);
			$this->masterbarangdetail_model->setProductname($_POST['nmbrng']);
            $this->masterbarangdetail_model->setTypeunit($_POST['tpunit']);
			$this->masterbarangdetail_model->setDescription($_POST['description']);
			$this->masterbarangdetail_model->setItemprice($_POST['itmprice']);
 			$this->masterbarangdetail_model->setLastUpdateBy($this->session->userdata('userid'));
			$tanggal=date('Y-m-d H:i:s', strtotime('now'));
			$this->masterbarangdetail_model->setLastUpdateOn($tanggal);
			
            $this->masterbarangdetail_model->ubahdetailbarang();

            redirect('masterbarangdetailcontroller/viewdetail/'.$_POST['kdbrng']);
        } 
		else 
		{
            $this->masterbarangdetail_model->setKodeDetailBarang($idkddet);
            $dataprofile = array(
				'page_title' => 'Master > Barang Detail',
				'name' => $this->session->userdata('fullname'),
				'mstrbarangdet' => $this->masterbarangdetail_model->getBarangByKode()			
			);		
			$content = array(
				'content' => 'master/barang/editdetail'
			);
			$this->template->load('templates/default',$content,$dataprofile);
		}
	}
	
	function deletedetailbarang() {
        $kdbrngdet = $this->uri->segment(3);
		$kdbrng = $this->uri->segment(4);
        $this->masterbarangdetail_model->setKodeDetailBarang($kdbrngdet);
        $this->masterbarangdetail_model->deletedetailbarang();
        redirect('masterbarangdetailcontroller/viewdetail/'.$kdbrng);
    }
}
?>